/*
 * Copyright 2008-2009 ArtisanLogic.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.artisanlogic.jingo.assembler;

import static com.artisanlogic.lang.Utils.requireParameter;

// =====================================================================================================================
// Dependency.java                                                                               Created on Feb 27, 2009
// ---------------------------------------------------------------------------------------------------------------------
/**
 * This class represents repository prefixed Jingo module dependency declarations.
 * 
 * @author john@artisanlogic.com
 * @author sean@artisanlogic.com
 * @since 0.9-Beta
 */
// =====================================================================================================================
public class Dependency implements Comparable<Dependency> {
	
  // -------------------------------------------------------------------------------------------------------------------
  // private instance fields
  // -------------------------------------------------------------------------------------------------------------------
  
	private final String repository;
	
	private final String moduleName;
	
	// -------------------------------------------------------------------------------------------------------------------
  // constructors
  // -------------------------------------------------------------------------------------------------------------------

	/**
	 * This constructor creates a new Dependency with the specified values for the repository and name properties.
	 *
	 * @param repository This parameter holds the repository identifier for this Jingo module dependency declaration.
	 * @param moduleName This parameter holds the module identifier for this Jingo module dependency declaration.
	 */
	public Dependency(final String repository, final String moduleName) {
	  requireParameter("repository", repository);
		this.repository = repository;
    requireParameter("moduleName", moduleName);
		this.moduleName = moduleName;
	}
	
	// -------------------------------------------------------------------------------------------------------------------
  // public instance methods
  // -------------------------------------------------------------------------------------------------------------------

  /**
   * This method provides access to the repository property.
   *
   * @return This method returns the value of the repository property.
   */
  public String getRepository() {
    return repository;
  }

  /**
   * This method provides access to the moduleName property.
   *
   * @return This method returns the value of the moduleName property.
   */
  public String getModuleName() {
    return moduleName;
  }
	
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return toString().hashCode();
	}

  /* (non-Javadoc)
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
	public boolean equals(Object other) {
		if(other == this) {
			return true;
		}
		if(other == null || !other.getClass().equals(getClass())) {
			return false;
		}
		Dependency otherDependency = (Dependency)other;
		return getModuleName().equals(otherDependency.getModuleName()) && 
			getRepository().equals(otherDependency.getRepository());
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Dependency other) {
		if(other == this) {
			return 0;
		} 
		if(other == null) {
			return -1;
		}
		return getModuleName().compareTo(other.getModuleName());
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return getRepository() + ":" + getModuleName();
	}
	
	// -------------------------------------------------------------------------------------------------------------------
  // public static methods
  // -------------------------------------------------------------------------------------------------------------------

	/**
	 * This method derives a dependency instance from a Jingo module dependency declaration that optionally 
	 * repository-prefixed.
	 * 
	 * @param dependency This parameter holds a string representation of the Jingo module declaration.
	 * @param defaultRepo The parameter holds the default repository identifier to be used for non-prefixed dependency 
	 *   declarations.
	 * @return This method returns a Dependency instance that corresponds to the provided dependency declarations and 
	 *   default repository.
	 */
	public static Dependency valueOf(final String dependency, final String defaultRepo) {
		String[] parts = dependency.split(":");
		if(parts.length > 1) {
			return new Dependency(parts[0], parts[1]);
		} else {
			return new Dependency(defaultRepo, parts[0]);
		}
	}
}